import axios from 'axios'
import { ElNotification } from 'element-plus'
import 'element-plus/es/components/notification/style/css'

import { useUserInfoStore } from '@/stores/userinfo'

var userinfoStore = useUserInfoStore()
//1.实例化axios
var service = axios.create({
    timeout: 10 * 1000,//超时时间
    baseURL: '',//基准路径
})
// axios.defaults.headers['Content-Type'] = ''
//2.设置请求拦截器
service.interceptors.request.use(
    (config:any) => {
        //设置请求头参数
        // config.headers['token'] = userinfoStore.userinfo.token;
        // config.headers["Content-Type"] === "application/json"
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
)

//3.设置响应拦截器
service.interceptors.response.use(
    (res) => {
        return res;
    },
    (error) => {
        if (error.response.status == 401) {
            ElNotification.error({ message: '登陆过期,请重新登录' });
        } else if (error.response.status == 404) {
            ElNotification.error({ message: '访问路径有误!' });
        } else if (error.response.status == 500) {
            ElNotification.error({ message: '服务器内部错误!' });
        } else if (error.response.status == 503) {
            ElNotification.error({ message: '服务器不可用!' });
        }
        return Promise.reject(error);
    }
)

export default service;